#include "movebutton.h"

MoveButton::MoveButton(QWidget *parent) : QPushButton(parent)
  ,parent(parent)
  ,pressed(false)
  ,alreadyMove(false)
  ,direction(3)
  ,moveEnable(true)
{
    setMouseTracking(true);
}

void MoveButton::mousePressEvent(QMouseEvent *e)
{
    if (moveEnable){
        if (e->button()==Qt::LeftButton){
            pressed = true;
            alreadyMove = false;
            pressPoint = QCursor::pos()-pos();
        }
    }
}

void MoveButton::mouseMoveEvent(QMouseEvent *e)
{
    if (moveEnable){
        if (pressed){
            move(QCursor::pos()-pressPoint);
            alreadyMove = true;
            emit moved();
        }
    }
    QPushButton::mouseMoveEvent(e);
}

void MoveButton::mouseReleaseEvent(QMouseEvent *e)
{
    if (moveEnable){
        pressed = false;
        if (!alreadyMove && e->button()==Qt::LeftButton){
            emit clicked(true);
            alreadyMove = false;
            QPushButton::mouseReleaseEvent(e);
            return;
        }
        changePos();
        alreadyMove = false;
    }
    QPushButton::mouseReleaseEvent(e);
}

/**
 * @brief moveEnabled       是否可移动
 * @return
 */
bool MoveButton::moveEnabled() const
{
    return moveEnable;
}

void MoveButton::setMoveEnable(bool value)
{
    moveEnable = value;
}

/**
 * @brief setDirection      设置停靠方向
 * @param value
 */
void MoveButton::setDirection(int value)
{
    direction = value;
}

/**
 * @brief changePos         当坐标改变的时候，重新计算位置
 */
void MoveButton::changePos()
{
    if (pos().x()<-width()/2){
        move(-width()*2/3, pos().y());
        direction = 3;
        emit moved();
        emit docked(direction);
    } else if (pos().x()>parent->width()+width()/2){
        move(parent->width()-width()/3,pos().y());
        direction = 4;
        emit moved();
        emit docked(direction);
    }else if (pos().y()<-height()/2){
        move(pos().x(),-height()*2/3);
        direction = 1;
        emit moved();
        emit docked(direction);
    }else if (pos().y()>parent->height()+height()/2){
        move(pos().x(),parent->height()-height()/3);
        direction = 2;
        emit moved();
        emit docked(direction);
    }else {
        switch(direction){
        case 1:
        case 2:
            if (pos().y()>=parent->height()/2){
                move(pos().x(),parent->height()-height());
                direction = 2;
                emit docked(direction);
            }else {
                move(pos().x(),0);
                direction = 1;
                emit docked(direction);
            }
            emit moved();
            break;
        case 3:
        case 4:
            if (pos().x()>=parent->width()/2){
                move(parent->width()-width(),pos().y());
                direction = 4;
                emit docked(direction);
            }else {
                move(0,pos().y());
                direction = 3;
                emit docked(direction);
            }
            emit moved();
            break;
        }
    }
}
